package com.codingfuture.glacierhardware.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.util.PageDate;
import com.codingfuture.glaciercommon.util.PageParam;
import com.codingfuture.glaciercommon.util.Result;
import com.codingfuture.glacierhardware.dto.WareHouseDTO;
import com.codingfuture.glacierhardware.entity.WareHouse;
import com.codingfuture.glacierhardware.service.WareHouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * @Author: HanLe
 * @DateTime: 2025/4/1 14:13
 * @Description:
 **/
@RestController
//@RequestMapping("/glacier-hardware")
@Slf4j
@RefreshScope
public class WareHouseController {
    @Autowired
    public WareHouseService WareHouseService;
    
    @GetMapping("/warehouse/selectByPage")
    public Result<PageDate<List<WareHouse>>> selectByPage(String name, PageParam pageParam){
        Page<WareHouse> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        WareHouseService.selectByPage(name,page);
        PageDate<List<WareHouse>> pageDate = new PageDate<>();

        pageDate.setRows(page.getRecords());
        pageDate.setTotal(page.getTotal());
        log.debug("selectByPage{}",pageParam);
        return Result.successes(pageDate);
    }

    @GetMapping("/warehouse/type")
    public Result<List<Map<String, Object>>> getTypes() {
        List<Map<String, Object>> types = Arrays.asList(
                Map.of("value", 0, "label", "冷库"),
            Map.of("value", 1, "label", "恒温仓")
        );
        return Result.successes(types);
    }

    @GetMapping("/warehouse/state")
    public Result<List<Map<String, Object>>> getStates() {
        List<Map<String, Object>> states = Arrays.asList(
            Map.of("value", 0, "label", "正常"),
            Map.of("value", 1, "label", "空仓")
        );
        return Result.successes(states);
    }

    @PostMapping("/warehouse/add")
    public Result<Void> add(@RequestBody WareHouseDTO warehouseDTO){
        WareHouse warehouse = warehouseDTO.convert2Entity();
        WareHouseService.add(warehouse);
        return Result.success();
    }

    @GetMapping("/warehouse/findById")
    public Result<WareHouse> findById(@RequestParam(value = "id") Long id) {
        WareHouse warehouse = WareHouseService.findById(id);
        return Result.successes(warehouse);
    }

    @PostMapping("/warehouse/updateById")
    public Result<Void> updateById(@RequestBody WareHouseDTO warehouseDTO){
        WareHouse warehouse = warehouseDTO.convert2Entity();
        WareHouseService.updateById(warehouse.getId(), warehouse);
        return Result.success();
    }

    @PostMapping("/warehouse/deleteById")
    public Result<Void> deleteById(@RequestParam(value = "id") Long id){
        WareHouseService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/warehouse/updateLocation")
    public Result<Void> updateLocation(@RequestBody WareHouseDTO warehouseDTO) {
        try {
            WareHouse warehouse = warehouseDTO.convert2Entity();
            WareHouseService.updateLocation(warehouse.getId(), warehouse.getLongitude(), warehouse.getLatitude());
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新位置失败：" + e.getMessage());
        }
    }
}
